package com.hr.controller;

import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.hr.entity.AoaUser;
import com.hr.feign.user.OaUserFeignService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 登录控制器
 */
@RestController
public class LoginController {

    @Autowired
    private OaUserFeignService oaUserFeignService;

    /**
     * 登录接口
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<AoaUser> login(
            @RequestBody AoaUser user
            ){
        //通行令
        UsernamePasswordToken token = new UsernamePasswordToken(
                //用户输入用户名
                user.getUserName(),
                //用户输入的密码
                user.getPassword()
        );
        //登录
        SecurityUtils.getSubject().login(token);
        ResponseEntity<AoaUser> resp = oaUserFeignService.queryByUserName(user.getUserName());
        return ResponseEntity.ok(resp.getBody());
    }

    /**
     * 测试接口 弄懂整个调用逻辑 可删除
     * @return
     */
    @RequiresPermissions("oa:system:manager")
    @GetMapping("/test1")
    public ResponseEntity<String> test1(){
        return ResponseEntity.ok("*** 你已经拥有系统管理的接口调用权限");
    }

}
